/**
 * 
 */
package modele.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author thameralhathal
 *
 */
public class ConnexionManager {
	
	public String userBdd;
	public String passwordBdd;
	public String port;
	public String serverName;
	public String bddName;
	public Connection connecteur;

	
	/**
	 * Constructeur permettant dinitialiser le parametre de connexion
	 * @param userBdd
	 * @param passwordBdd
	 * @param port
	 * @param serverName
	 * @param bddName
	 */
	public ConnexionManager(String userBdd, String passwordBdd, String port,
			String serverName, String bddName) {
		this.userBdd = userBdd;
		this.passwordBdd = passwordBdd;
		this.port = port;
		this.serverName = serverName;
		this.bddName = bddName;
	}


	public void openBDD() throws SQLException, ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		System.out.println("DRIVER OK ! ");
		StringBuilder urlBuilder = new StringBuilder("jdbc:mysql://");
		urlBuilder.append(serverName).append(":");
		urlBuilder.append(port).append("/");
		urlBuilder.append(bddName);
		String url = urlBuilder.toString();
		
		
		connecteur = DriverManager.getConnection(url, userBdd, passwordBdd);
		System.out.println("Connection effective !");	
		 
	}
	public boolean closeBdd(){
		try {
			connecteur.close();
			System.out.println("fermeture de la base avec succes");
			return true;
		} catch (SQLException e) {
			System.err.println("erreur de cloture de la base");
			return false;
		}
	}
	
	public static void main(String[] args) {
		//connexion locale
		ConnexionManager connector = new ConnexionManager("essai","essai","3306","localhost", "kebab");
		
		try {
			connector.openBDD();
		} catch (Exception e) {
			System.err.println("erreur de connection a la base de donnee");
		} 
	}
}
